//
//  GQHEnterpriseAuthorizationView.m
//  Xanthium
//
//  Created by GuanQinghao on 31/03/2018.
//  Copyright © 2018 GuanQinghao. All rights reserved.
//

#import "GQHEnterpriseAuthorizationView.h"
#import "GQHCustomCell.h"
#import "GQHHeader.h"
#import "GQHBaseModel.h"

@interface GQHEnterpriseAuthorizationView () <UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate> {
    
    NSArray *titleArray;
    UIButton *doneButton;
    NSArray *_placeHolderArray;
}

@end


@implementation GQHEnterpriseAuthorizationView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        [self loadLocalData];
        [self loadUserInterface];
    }
    return self;
}

#pragma mark --Data
- (void)loadLocalData {
    
    titleArray = @[@"公司名称",@"公司地址",@"联系人",@"联系电话",@"身份证号",@"营业执照编号",@"营业执照",@"道路运输经营许可证"];
    _qh_contentArray = [NSMutableArray arrayWithArray:@[@"",@"",@"",@"",@"",@"",@"请输入",@"请输入"]];
    _placeHolderArray = @[@"请输入",@"请输入",@"请输入",@"请输入",@"请输入",@"请输入",@"",@""];
}

#pragma mark --View
- (void)loadUserInterface {
    
    _qh_tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    _qh_tableView.delegate = self;
    _qh_tableView.dataSource = self;
    _qh_tableView.backgroundColor = kWhiteColor;
    _qh_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _qh_tableView.showsVerticalScrollIndicator = NO;
    if (@available(iOS 11.0, *)) {
        
        _qh_tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    [self addSubview:_qh_tableView];
    [_qh_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.qh_statusBarHeight + self.qh_navigationBarHeight);
        make.left.and.right.and.bottom.mas_equalTo(self);
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return titleArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (6 == indexPath.row || 7 == indexPath.row)
    {
        return 60.0f;
    } else {
        
        return 50.0f;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (6 == indexPath.row || 7 == indexPath.row)
    {
        static NSString *identifier = @"bCell";
        GQHTruckImagesCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (!cell) {
            
            cell = [[GQHTruckImagesCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        }
        
        cell.qh_captionLabel.text = titleArray[indexPath.row];
        cell.qh_rightImageView.backgroundColor = kLightGrayColor;
        
        return cell;
    }
    else
    {
        
        static NSString *identifier = @"aCell";
        YYEnterTextCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (!cell) {
            
            cell = [[NSBundle mainBundle] loadNibNamed:@"YYEnterTextCell" owner:self options:nil].firstObject;
        }
        
        cell.titleLabel.text = titleArray[indexPath.row];
        cell.enterText.text = _qh_contentArray[indexPath.row];
        cell.enterText.placeholder = _placeHolderArray[indexPath.row];
        cell.enterText.delegate = self;
        cell.enterText.tag = indexPath.row;
        cell.isEnter = YES;
        cell.perLabel_width.constant = 0;
        
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.row > 5)
    {
        if ([_qh_delegate respondsToSelector:@selector(qh_selectedRowWithTableView:atIndexPath:)]) {
            
            [_qh_delegate qh_selectedRowWithTableView:tableView atIndexPath:indexPath];
        }
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return CGFLOAT_MIN;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return 90.0f;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    
    UIView *footerView = [[UIView alloc] init];
    
    CGSize textSize = [GQHBaseModel sizeWithString:@"我已阅读并同意《智运平台物流企业认证协议》" font:[UIFont systemFontOfSize:12.0f] maxSize:CGSizeMake(self.qh_width, 20)];
    UIButton *agreementButton = [UIButton buttonWithType:UIButtonTypeCustom];
    agreementButton.titleLabel.font = [UIFont systemFontOfSize:12.0f];
    [agreementButton setTitle:@"我已阅读并同意《智运平台物流企业认证协议》" forState:UIControlStateNormal];
    [agreementButton setTitleColor:kDarkBlackColor forState:UIControlStateNormal];
    [agreementButton addTarget:self action:@selector(checkAgreement:) forControlEvents:UIControlEventTouchUpInside];
    [footerView addSubview:agreementButton];
    [agreementButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.size.mas_equalTo(CGSizeMake(textSize.width + 10, 20));
        make.centerX.mas_equalTo(footerView).offset(-20);
        make.top.mas_equalTo(footerView).with.inset(10);
    }];
    
    UIColor * color = [UIColor colorWithRed:25/255.0 green:182/255.0 blue:15/255.0 alpha:1.0];
    CGSize textSize1 = [GQHBaseModel sizeWithString:@"查看协议" font:[UIFont systemFontOfSize:12] maxSize:CGSizeMake(self.qh_width, 20)];
    UIButton *viewAgreement = [UIButton buttonWithType:UIButtonTypeCustom];
    viewAgreement.titleLabel.font = [UIFont systemFontOfSize:12.0f];
    [viewAgreement setTitle:@"查看协议" forState:UIControlStateNormal];
    [viewAgreement setTitleColor:color forState:UIControlStateNormal];
    [viewAgreement addTarget:self action:@selector(checkAgreement:) forControlEvents:UIControlEventTouchUpInside];
    [footerView addSubview:viewAgreement];
    [viewAgreement mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.width.mas_equalTo(textSize1.width + 10);
        make.height.mas_equalTo(20);
        make.left.mas_equalTo(agreementButton.mas_right);
        make.top.mas_equalTo(footerView).with.inset(10);
    }];
    
    UIButton *checkButton = [UIButton buttonWithType:UIButtonTypeCustom];
    checkButton.backgroundColor = [UIColor whiteColor];
    checkButton.layer.cornerRadius = 3.0;
    checkButton.layer.borderWidth = 1.0;
    checkButton.layer.borderColor = kAppRedColor.CGColor;
    checkButton.layer.masksToBounds = YES;
    checkButton.selected = YES;
    [checkButton setImage:[UIImage imageNamed:@"login_register_agreement"] forState:UIControlStateSelected];
    [checkButton setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    [checkButton addTarget:self action:@selector(agreeWithAgreement:) forControlEvents:UIControlEventTouchUpInside];
    [footerView addSubview:checkButton];
    [checkButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.size.mas_equalTo(CGSizeMake(15, 15));
        make.centerY.mas_equalTo(agreementButton);
        make.right.mas_equalTo(agreementButton.mas_left);
    }];
    
    doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
    doneButton.backgroundColor = kAppRedColor;
    doneButton.layer.cornerRadius = 5.0f;
    doneButton.layer.masksToBounds = YES;
    doneButton.titleLabel.font = kDefaultFont;
    [doneButton setTitle:@"完成" forState:UIControlStateNormal];
    [doneButton setTitleColor:kWhiteColor forState:UIControlStateNormal];
    [doneButton addTarget:self action:@selector(submitInformation:) forControlEvents:UIControlEventTouchUpInside];
    [footerView addSubview:doneButton];
    [doneButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(agreementButton.mas_bottom).with.inset(10);
        make.bottom.mas_equalTo(footerView).with.inset(10);
        make.left.and.right.mas_equalTo(footerView).with.inset(20);
    }];
    
    return footerView;
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    [_qh_contentArray replaceObjectAtIndex:textField.tag withObject:textField.text];
}

- (IBAction)agreeWithAgreement:(UIButton *)sender {
    DLog(@"选中同意协议内容");
    
    sender.selected = !sender.selected;
    if (sender.selected) {
        
        doneButton.userInteractionEnabled = YES;
        [doneButton setTitleColor:kWhiteColor forState:UIControlStateNormal];
    } else {
        
        doneButton.userInteractionEnabled = NO;
        [doneButton setTitleColor:kGrayColor forState:UIControlStateNormal];
    }
}

- (IBAction)checkAgreement:(UIButton *)sender {
    DLog(@"查看认证协议内容");
    
    if ([_qh_delegate respondsToSelector:@selector(qh_checkAgreement)]) {
        
        [_qh_delegate qh_checkAgreement];
    }
}

- (IBAction)submitInformation:(UIButton *)sender {
    DLog(@"提交认证信息");
    
    if ([_qh_delegate respondsToSelector:@selector(qh_submitInformation)]) {
        
        [_qh_delegate qh_submitInformation];
    }
}

@end
